package com.android.myfilm.activities;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.myfilm.R;
import com.android.myfilm.pojo.MovieResultItem;


public class MyCustomArrayAdapter extends ArrayAdapter {
	
	private static List<MovieResultItem> searchArrayList;
	private LayoutInflater mInflater;
	private static Map<String, Bitmap> cache = new HashMap<String, Bitmap>();
	
	@SuppressWarnings("unchecked")
	public MyCustomArrayAdapter(Context context, int textViewResourceId, List<MovieResultItem> results, Map<String, Bitmap> cache) {
		super(context, textViewResourceId, results);
		
		MyCustomArrayAdapter.cache = cache;
		searchArrayList = results;
		mInflater = LayoutInflater.from(context);
	}
	
	public int getCount() {
		return searchArrayList.size();
	}
	
	public Object getItem(int position) {
		return searchArrayList.get(position);
	}
	
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.custom_row_view, null);
			holder = new ViewHolder();
			holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
			holder.poster = (ImageView) convertView.findViewById(R.id.poster);
			holder.title = (TextView) convertView.findViewById(R.id.titleAndYear);
			holder.actors = (TextView) convertView.findViewById(R.id.actors);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		try {
			String posterURL = searchArrayList.get(position).getPoster();
			
			if (cache.get(posterURL) == null) {
				Bitmap b = BitmapFactory.decodeStream(new URL(posterURL).openStream());
				
				/** Create bitmap object from URL and scale if URL contains string "default"*/
				if (posterURL != null && posterURL.contains("default")) {
					Bitmap b_scaled = Bitmap.createScaledBitmap(b, 61, 91, false);
					holder.poster.setImageBitmap(b_scaled);
					cache.put(posterURL, b_scaled);
				}
				else {
					holder.poster.setImageBitmap(b);
					cache.put(posterURL, b);
				}
			}
			else {
				holder.poster.setImageBitmap(cache.get(posterURL));
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		holder.title.setText(searchArrayList.get(position).getTitle());
		holder.actors.setText(searchArrayList.get(position).getActors());
		holder.layout.setTag(searchArrayList.get(position).getId());
		
		return convertView;
	}
	
	static class ViewHolder {
		ImageView poster;
		TextView title;
		TextView actors;
		LinearLayout layout;
	}
}
